<?PHP
	import("view.HTMLTag");
	function linkTo($name,$path,$options=array()){
	
		if(!isset($path) || $path=="") $path = APP_DEFAULT_ACTION;
		
		
		
		$result = "";
		$canExecute = true;
		if(APP_USE_SECURITY){
			$actionArray = explode("/",$path);
			$class = $actionArray[0]."Controller";
			$method = (count($actionArray)>1) ? $actionArray[1] : "index";
			$params = array();
			for ( $i=2; $i < count($actionArray); $i++ ){
				array_push($params,$actionArray[$i]);
			}
	
			import('application.controllers.'.strtolower($actionArray[0]));
			$process = new $class();
			$securityMethod = "getCredentials";
			if(APP_CUSTOM_SECURITY_HANDLER) $securityMethod = APP_CUSTOM_SECURITY_HANDLER;
			if(array_key_exists($method,$process->accessLevels)){
				$access_level = $process->accessLevels[$method];
			}else if(array_key_exists("default",$process->accessLevels)){
				$access_level = $process->accessLevels["default"];
			}else{
				$access_level = APP_DEFAULT_SECURITY_LEVEL;
			}
			if(defined('APP_CONTROLLER')){
				import('application.controllers.'.strtolower(APP_CONTROLLER));
				$appClass = APP_CONTROLLER."Application";
				$app = new $appClass();
				// FIXME: Sacar template
				//if(isset($template)) $app->setTemplate($template);
			}
			$canExecute = $app->$securityMethod($access_level,$path);
		}
		if($canExecute) {
			$result = link_tag($name,linkForm($path),$options);
		}
		/*if(defined('APP_METHOD_SECURITY_HANDLER')){
			$paths = explode("/",$path);
			$controller = array_shift($paths);
			$action = array_shift($paths);
			$params = join("/",$paths);
			import("application.controllers.".$controller);
			$class_name = $controller."Controller";
			$temp_class = new $class_name();
			$securityClass = APP_MAIN_CONTROLLER."Controller";
			$securityHandler = APP_METHOD_SECURITY_HANDLER;
			if(call_user_func_array(array($securityClass,$securityHandler),array($temp_class,$action))){
				$result = link_tag($name,linkForm($path),$options);
			}
		}else{
			$result = link_tag($name,linkForm($path),$options);
		}*/
		return $result;	
	}
	function linkForm($path){
		$result_url = (APP_USE_HUMAN_URLS) ? $path : APP_LOGIC_PROXY."?path=$path";
		return APP_ROOT.$result_url;
	}
	
	function imageTo($img,$path,$options=array()){
		return linkTo(image_tag($img),$path,$options=array());
	}
	function buttonTo($label,$path,$options=array()){
		$options["type"] = "button";
		$options["value"] = $label;
		$options["onclick"] = "window.location.href='".linkForm($path)."';";
		$redirect = new HTMLTag("input",$options);
		return $redirect->build();
	}
	function buttonBack($label,$options=array()){
		$options["type"] = "button";
		$options["value"] = $label;
		$options["onclick"] = "history.back();";
		$backbtn = new HTMLTag("input",$options);
		return $backbtn->build();
	}
	function buttonReset($label,$options=array()){
		$options["type"] = "reset";
		$options["value"] = $label;
		$reset = new HTMLTag("input",$options);
		return $reset->build();
	}
	function redirect($path,$time=0){
		$options = array();
		$options["http-equiv"] = "refresh";
		$options["content"] = $time."; URL=".linkForm($path);
		$meta = new HTMLTag("meta",$options);
		return $meta->build();
	}
?>